package com.douban.crawl.crawler;

import com.douban.crawl.db.BookCommentDao;
import com.douban.crawl.db.BookDao;
import com.douban.crawl.model.Book;
import com.douban.crawl.model.BookComment;
import com.douban.crawl.utils.CommonUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 *
 */
public class BookCommentCrawler {
    public static void main(String[] args) {

        BookCommentDao bookCommentDao = new BookCommentDao();

        CommonUtil.setGlobalIPAndPort();
        String header = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36";

        String url = "https://book.douban.com/subject/BOOK_ID/comments/";
        BookDao bookDao = new BookDao();
        List<Book> bookList = bookDao.getList();
        for (Book book : bookList) {
            String newUrl = url.replaceAll("BOOK_ID", book.getBook_id());
            try {
                extractComment(newUrl, header, book, bookCommentDao);
                TimeUnit.SECONDS.sleep(2);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static void extractComment(String url, String header, Book book, BookCommentDao bookCommentDao) throws IOException {
        Document doc = Jsoup.connect(url).header("User-Agent", header).get();
        Element commentsEle = doc.body().getElementById("comments");
        Elements commentItems = commentsEle.getElementsByClass("comment-item");
        for (Element commentEle : commentItems) {
            // 头像
            String headUrl = commentEle.getElementsByTag("img").get(0).attr("src");

            Element commentInfoEle = commentEle.getElementsByClass("comment-info").get(0);
            // 昵称
            String nikeName = commentInfoEle.getElementsByTag("a").text();
            // 时间
            String time = commentInfoEle.getElementsByClass("comment-time").get(0).text();
            // 评价
            String comment = commentEle.getElementsByClass("comment-content").get(0).text();

            StringBuilder res = new StringBuilder();
            res.append("id:").append(book.getBook_id()).append(System.lineSeparator())
                    .append("头像:").append(headUrl).append(System.lineSeparator())
                    .append("昵称:").append(nikeName).append(System.lineSeparator())
                    .append("时间:").append(time).append(System.lineSeparator())
                    .append("评价:").append(comment).append(System.lineSeparator());
            System.out.println(res);

            BookComment bookComment = new BookComment(headUrl, nikeName,
                    time, comment, book.getBook_id());

            bookCommentDao.save(bookComment);
        }
    }
}
